package GUI_Data.User;

import Data_Flow.GetData;
import Data_Structure.DataType.MyFlight;
import GUI_Data.FlightSeatPanel.SeatButton;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
/**
 * This class contains all the Labels, text fields and the variables needed in GUI logic.
 * @author Xiaorun Wang
 * @author Chunxi Lin
 * @author Yibai Jiang
 */
@Data
public class AllLabelAndTextField {
    private JLabel amount = new JLabel("The summary of the booked flight");
    private Object[][] flightList;
    private static final Object[] flightListColumnTitle = {"Flight number","Flight ID","Start Point","Dest Point","Boarding Time"};
    private JTable flightTable;

    public JLabel input_bookingNum = new JLabel("Please enter your flight number:");
    public JLabel input_Sur_ID = new JLabel("Please enter your Surname and ID:");
    public JLabel input_Scan = new JLabel("Please enter your ticket:");

    private JTextField num = new JTextField(80);

    private JTextField sur = new JTextField(80);
    private JTextField id = new JTextField(80);

    private JLabel displayLabel = new JLabel("Flight information");

    private JLabel payLabel = new JLabel("Need to pay:");
    private static final Object[] payListTitle = {"No.","Payment","Number","Unit price"};
    private Object[][] payList;
    private JTable payTable;
    private int cost = 0;

    private JLabel total = new JLabel("Total: ");

    private static final Object[] printListTitle = {"Boarding Pass","Bag tag","Ticket"};
    private static Object[][] printList = {{"Need to print","Need to print","Need to print"}};
    private JTable printTable = new JTable(printList,printListTitle);
    private JLabel pass = new JLabel("Boarding Pass");

    private JLabel bag = new JLabel("Bag tag");

    private JLabel ticket = new JLabel("Ticket");

    private JLabel printLabel = new JLabel("Printing");

    private JLabel payLabel2 = new JLabel("Paying");

    private JLabel label = new JLabel("Check-In completed! Have a nice trip and enjoy the flight!");

    private JLabel welcome = new JLabel("Welcome");

    private JLabel flightNum = new JLabel("Flight number:");
    private JLabel status = new JLabel("Flight status:");
    private JLabel departTime = new JLabel("Departure time:");
    private JLabel departGate = new JLabel("Departure gate:");
    private JLabel seat = new JLabel("Seat:");
    private JLabel counter = new JLabel("Check-in counter:");
    private JLabel food = new JLabel("Food and Beverage:");

    private JLabel seatChooseLabel = new JLabel();
    private JLabel extraSeatChooseLabel = new JLabel();

    /**
     * The generator of AllLabelAndTextField
     * Initialize all labels and text fields on the login page.
     * @param userFrame The userFrame which contains this class
     */
    public AllLabelAndTextField(UserFrame userFrame){
        Font fontInput = new Font("Arial", Font.PLAIN, 50);
        input_bookingNum.setFont(fontInput);
        input_Sur_ID.setFont(fontInput);
        displayLabel.setFont(fontInput);
        payLabel.setFont(fontInput);
        printLabel.setFont(fontInput);
        label.setFont(fontInput);

        Font fontInformation = new Font("Arial", Font.PLAIN, 30);
        flightNum.setFont(fontInformation);
        status.setFont(fontInformation);
        departTime.setFont(fontInformation);
        departGate.setFont(fontInformation);
        seat.setFont(fontInformation);
        counter.setFont(fontInformation);
        food.setFont(fontInformation);


        total.setFont(new Font("Arial", Font.PLAIN, 35));

        num.setBorder(BorderFactory.createTitledBorder("Booking number:"));
        sur.setBorder(BorderFactory.createTitledBorder("Surname:"));
        id.setBorder(BorderFactory.createTitledBorder("ID number:"));

    }

    /**
     * This method is called when you log in using the second or third login methods.
     * Used to generate a flight list for selection
     * @param getData Used to generate a flight list for selection.
     */
    public void preInit(GetData getData){
        flightList = new Object[getData.getPersonFlightList().size()][];
        int index = 0;
        for(HashMap.Entry<String, MyFlight> entry: getData.getPersonFlightList().entrySet()) {
            Object[] oneFlight = {entry.getKey(),entry.getValue().getMyFlightID(),
                    entry.getValue().getMyStartPoint(),entry.getValue().getMyDestPoint(),
                    entry.getValue().getMyBoardingTime().toString()};
            flightList[index] = oneFlight;
            index++;
        }
        flightTable = new JTable(flightList,flightListColumnTitle);
    }

    /**
     * Used to generate information display labels.
     * @param getData Used to generate a flight list for selection.
     */
    public void init(GetData getData){

        flightNum.setText("Flight number:" + getData.getMyFlightID());
        status.setText("Flight status:" + getData.getMyFlightStatus());
        departTime.setText("Departure time:" + getData.getMyBoardingTime());
        departGate.setText("Departure gate:" + getData.getMyGate());
        counter.setText("Check-in counter:");

        seatChooseLabel.setText(getData.getMyPlaneType().toString());
        extraSeatChooseLabel.setText(getData.getMyPlaneType().toString());
    }

    /**
     * The form used to create the payment interface.
     * Called when jumping to the payment page.
     * @param dimension All the dimension information.
     * @param allPanel All the JPanel objects.
     * @param getData The object to get data according to the login account.
     * @param targetButton The target seat button.
     */
    public void createPayTable(SetDimension dimension,AllPanel allPanel, GetData getData, SeatButton targetButton){
        int extraMealNumber = 0;
        for (int i = 0; i < allPanel.getExtraMealsChooseNum().length; i++) {
            if (allPanel.getExtraMealsChooseNum()[i] != 0){
                extraMealNumber++;
            }
        }
        if (targetButton.getSeatClass().compareTo(getData.getMySeatClass()) < 0){
            int upgradeCost = GetData.getSeatPrice(getData.getMyPlaneType(),targetButton.getSeatClass()) - getData.getMySeatPrice();
            cost += upgradeCost;
            Object[] upgradePayment= {extraMealNumber + 1,getData.getMySeatClass() + "->" + targetButton.getSeatClass(),"1",upgradeCost};
            payList = new Object[extraMealNumber + 1][];
            payList[extraMealNumber] = upgradePayment;
        }else
            payList = new Object[extraMealNumber][];

        int index = 0;
        for (int i = 0; i < allPanel.getExtraMealsChooseNum().length; i++) {
            if (allPanel.getExtraMealsChooseNum()[i] != 0){
                Object[] mealItem = {index + 1,allPanel.getExtraMealsName()[i],allPanel.getExtraMealsChooseNum()[i],allPanel.getExtraMealsPrice()[i] * allPanel.getExtraMealsChooseNum()[i]};
                cost += allPanel.getExtraMealsPrice()[i] * allPanel.getExtraMealsChooseNum()[i];
                payList[index++] = mealItem;
            }
        }
        total.setText("Total: " + cost + "  pounds");
        payTable = new JTable(payList,payListTitle);
        payTable.setOpaque(false);
        payTable.setRowHeight((int)(dimension.getScreenHeight()*0.75*0.6)/(extraMealNumber+2));
        payTable.setPreferredSize(dimension.getPayTableDimension());
    }
}
